//
//  GCPresenter.m
//  CBHGroupCar
//
//  Created by mac on 2018/1/24.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GCPresenter.h"
#import "AppDelegate.h"
#import "Head.h"

@interface GCPresenter()
@property (nonatomic,weak) AppDelegate *appDelegate;
@property (nonatomic,strong) GYLeadVC *leadVC;
@end

@implementation GCPresenter
#pragma mark - getter and setter
-(GYMainTab *)mainVC
{
    if (!_mainVC) {
        _mainVC = [[GYMainTab alloc]init];
        [self requestLastVersion];
    }
    return _mainVC;
}
-(AppDelegate *)appDelegate
{
    if (!_appDelegate) {
        _appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    }
    return _appDelegate;
}
-(GYLeadVC *)leadVC
{
    if (!_leadVC) {
        _leadVC = [GYLeadVC new];
    }
    return _leadVC;
}
#pragma mark - method
-(void)loadMain
{
    // 设置您在百度移动统计网站上添加的APP的AppKey, 此处AppId即为应用的AppKey
    [[BaiduMobStat defaultStat] startWithAppId:@"f9f633136d"];
    
    GYLoginInfoModel* model=[[GYLoginViewModel shareInterface] loginInfoModel];
    if (model.loginmobile) {
        
        [[GYLoginViewModel shareInterface] requestDefaultLogin];
    }else
    {
        
        
//        GYChooseRoleVC *login = [[GYChooseRoleVC alloc]init];
//        HMSNavigationController *nav = [[HMSNavigationController alloc]initWithRootViewController:login];
        self.appDelegate.window.rootViewController = self.mainVC;
    }

}
-(void)loadChooseVC
{

}
-(void)loadLead
{
    self.appDelegate.window.rootViewController = self.leadVC;
}
-(void)loadLogin
{
    GYLoginVC *login = [[GYLoginVC alloc]init];
//    [self.navigationController pushViewController:login animated:YES];
//    GYChooseRoleVC *login = [[GYChooseRoleVC alloc]init];
    HMSNavigationController *nav = [[HMSNavigationController alloc]initWithRootViewController:login];
    [self.appDelegate.window.rootViewController presentViewController:nav animated:YES completion:nil];
}

-(void)generalConfig
{
    //设置键盘事件
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.shouldResignOnTouchOutside = YES;
    //    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.preventShowingBottomBlankSpace = YES;
    manager.toolbarTintColor = Main_Color;
    manager.toolbarDoneBarButtonItemText = @"完成";
    [[UITextField appearance] setTintColor:Main_Color];
    [[UITextView appearance]setTintColor:Main_Color];
    
    //阿里云文件上
     [[GYAliyunOSS sharedInstance] setupEnvironment];
    

}
-(void)requestLastVersion
{

}
-(void)phoneCall:(NSString *)phone
{
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
-(void)showOriginalImages:(NSArray *)originalImages andImageViews:(NSArray *)sourceImgageViews withIndex:(NSInteger)index
{
    // 1. 创建photoBroseView对象
    PYPhotoBrowseView *photoBroseView = [[PYPhotoBrowseView alloc] init];
    
    // 2.1 设置图片源(UIImageView)数组
    photoBroseView.imagesURL = originalImages;
    if (sourceImgageViews) photoBroseView.sourceImgageViews = sourceImgageViews;
    photoBroseView.currentIndex = index;
    photoBroseView.frameFormWindow = CGRectMake(0, 0, SCREEN_WIDTH, WFCGFloatY(200));
    photoBroseView.frameToWindow = photoBroseView.frameFormWindow;
    // 3.显示(浏览)
    [photoBroseView show];
}
-(void)showOriginalImages:(NSArray *)originalImages andImageViews:(NSArray *)sourceImgageViews
{
    [self showOriginalImages:originalImages andImageViews:sourceImgageViews withIndex:0];
}
-(void)shareContent:(NSString *)content from:(UIViewController *)viewController
{
    TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:[GYShareView shareInterface] preferredStyle:TYAlertControllerStyleActionSheet];
    alertController.backgoundTapDismissEnable = YES;
    [viewController presentViewController:alertController animated:YES completion:nil];

}
#pragma mark - life
+(instancetype)shareInterface
{
    static GCPresenter *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [[GCPresenter alloc]init];
        }
    });
    return manager;
}
-(void)dealloc
{
    [[InterfaceManager shareInterface]removeObserver:self forKeyPath:@"netStatus"];
}
-(instancetype)init
{
    if (self = [super init]) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        UIWindow *window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        [window makeKeyWindow];
        [window makeKeyAndVisible];
        window.backgroundColor = Main_Color_White;
        self.appDelegate.window = window;
        [self generalConfig];
        if (!_appDelegate.window.rootViewController) {
            GCLaunchVC *vc = [[GCLaunchVC alloc]init];
            _appDelegate.window.rootViewController = vc;
        }
        //
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"getLastVersion"];
        [[InterfaceManager shareInterface]addObserver:self forKeyPath:@"netStatus" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
    }
    return self;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"netStatus"]) {
//        NetStatusT old = [change[@"old"] integerValue];
        NetStatusT new = [change[@"new"] integerValue];
        if (NetStatusWWAN == new) {
            [FTIndicator showToastMessage:@"您现在使用的是手机流量，请注意哦~"];
        }
        if (NetStatusWiFi == new) {
        }
        if (NetStatusReachable == new) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络不可用，请检查一下~" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {

            }];
        }
    }
}
@end


@interface GYShareView()
@property (nonatomic,strong) NSMutableArray *datas;
@end

@implementation GYShareView
#pragma mark - getter and setter
#pragma mark - event
#pragma mark - method
+(instancetype)shareInterface
{
    static GYShareView *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [GYShareView new];
        }
    });
    return manager;
}
#pragma mark - life
-(instancetype)init
{
    if (self = [super init]) {
        self.width = SCREEN_WIDTH;
        self.height = WFCGFloatY(151);
        self.backgroundColor = Main_Color_White;
        
        UIButton *btn1 = [UIButton new];
        btn1.frame = WFCGRectMake(94, 47, 71, 71);
        [btn1 setImage:[UIImage imageNamed:@"wx"] forState:UIControlStateNormal];
        [btn1 handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            if (_callback) {
                _callback(0);
            }
        }];
        [self addSubview:btn1];
        
        UIButton *btn2 = [UIButton new];
        btn2.frame = WFCGRectMake(217, 47, 71, 71);
        [btn2 setImage:[UIImage imageNamed:@"qq"] forState:UIControlStateNormal];
        [btn2 handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            if (_callback) {
                _callback(1);
            }
        }];
        [self addSubview:btn2];
    }
    return self;
}
@end
